package com.example.firststory;

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Personalizacion extends Activity {

	// Menu Principal
	ArrayList<ImageButton> principal = new ArrayList<ImageButton>();

	// Array auxiliar que se va a mostrar
	ArrayList<ImageButton> aux = new ArrayList<ImageButton>();

	// Objetos Menu 1
	ArrayList<ImageButton> menu1 = new ArrayList<ImageButton>();

	// Objetos Menu 2
	ArrayList<ImageButton> menu2 = new ArrayList<ImageButton>();

	// Objetos Menu 3
	ArrayList<ImageButton> menu3 = new ArrayList<ImageButton>();

	LinearLayout scrollMenu;

	ImageButton flecha, cuadrado, redonda;

	ImageButton volver;

	ImageView muestra;

	Button seguir;
	Intent traspaso;

	String menu1Name, menu2Name, menu3Name;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_personalizacion);

		try {
			getReferences();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}

		menuPrincipal();

		setSeguirClick();
		setVolverClick();

	}

	

	private void getReferences() throws IllegalAccessException,
			IllegalArgumentException {

		traspaso = new Intent(this, SeleccionarHistoria.class);

		scrollMenu = (LinearLayout) findViewById(R.id.hsMenu);

		flecha = (ImageButton) findViewById(R.id.ibMenu1);
		cuadrado = (ImageButton) findViewById(R.id.ibMenu2);
		redonda = (ImageButton) findViewById(R.id.ibMenu3);

		muestra = (ImageView) findViewById(R.id.ivMuestra);

		volver = (ImageButton) findViewById(R.id.pVolver);

		seguir = (Button) findViewById(R.id.pSeguir);

		getMenuNames();
		flecha.setOnClickListener(setListener(menu1Name, menu1, scrollMenu));

		redonda.setOnClickListener(setListener(menu2Name, menu2, scrollMenu));

		cuadrado.setOnClickListener(setListener(menu3Name, menu3, scrollMenu));

		principal.add(flecha);
		principal.add(cuadrado);
		principal.add(redonda);

		cogerImagenes(menu1Name, menu1);
		cogerImagenes(menu2Name, menu2);
		cogerImagenes(menu3Name, menu3);

	}

	private void setVolverClick() {

		volver.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				menuPrincipal();

			}
		});
	}

	private void menuPrincipal() {
		emptyLayout(scrollMenu);
		fullLayout(scrollMenu, principal);
	}

	protected void emptyLayout(LinearLayout hsvMenu) {

		hsvMenu.removeAllViews();

	}

	protected void fullLayout(LinearLayout hsvMenu, ArrayList<ImageButton> array) {

		emptyLayout(hsvMenu);

		for (int i = 0; i < array.size(); i++) {

			hsvMenu.addView(array.get(i));

		}

	}

	private void cogerImagenes(String nombre, ArrayList<ImageButton> temp)
			throws IllegalAccessException, IllegalArgumentException {

		R.drawable drawableResources = new R.drawable();
		Class<R.drawable> c = R.drawable.class;
		Field[] fields = c.getDeclaredFields();

		if (!nombre.equalsIgnoreCase("")) {

			for (int i = 0; i < fields.length; i++) {
				int id = fields[i].getInt(drawableResources);
				if (getResources().getResourceName(id).contains(nombre)) {

					if (!getResources().getResourceName(id).endsWith("menu")) {

						ImageButton boton1 = new ImageButton(this);
						boton1.setImageDrawable(getResources().getDrawable(id));
						boton1.setOnClickListener(setListenerBotonesIndividuales(boton1));
						temp.add(boton1);

					}
				}
			}

		} else {

			for (int i = 0; i < fields.length; i++) {

				int id = fields[i].getInt(drawableResources);

				ImageButton boton1 = new ImageButton(this);
				boton1.setImageDrawable(getResources().getDrawable(id));
				temp.add(boton1);

			}
		}

	}

	public View.OnClickListener setListener(final String str,
			final ArrayList<ImageButton> array, final LinearLayout layout) {

		return new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				try {

					array.clear();
					cogerImagenes(str, array);
					fullLayout(layout, array);

				} catch (IllegalAccessException e) {

					e.printStackTrace();
				} catch (IllegalArgumentException e) {

					e.printStackTrace();
				}
			}
		};
	}

	

	private void getMenuNames() throws IllegalAccessException,
			IllegalArgumentException {

		R.drawable drawableResources = new R.drawable();
		Class<R.drawable> c = R.drawable.class;
		Field[] fields = c.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			int id = fields[i].getInt(drawableResources);
			if (getResources().getResourceName(id).contains("flechamenu")) {

				menu1Name = getResources().getResourceName(id).substring(0,
						getResources().getResourceName(id).length() - 4);
				menu1Name = menu1Name.substring((menu1Name.indexOf("/") + 1));

			}
			if (getResources().getResourceName(id).contains("redondamenu")) {

				menu3Name = getResources().getResourceName(id).substring(0,
						getResources().getResourceName(id).length() - 4);
				menu3Name = menu3Name.substring((menu3Name.indexOf("/") + 1));

			}
			if (getResources().getResourceName(id).contains("cuadradomenu")) {

				menu2Name = getResources().getResourceName(id).substring(0,
						getResources().getResourceName(id).length() - 4);
				menu2Name = menu2Name.substring((menu2Name.indexOf("/") + 1));

			}
		}
	}

	public View.OnClickListener setListenerBotonesIndividuales(
			final ImageButton ib) {

		return new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				muestra.setImageDrawable(ib.getDrawable());

			}

		};
	}
	
	private void setSeguirClick() {

		seguir.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				setActivity(SeleccionarHistoria.class);
				cambiarActivity();
				
			}
		});
		
	}
	
	public void cambiarActivity() {

		traspaso.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(traspaso);

	}

	public void setActivity(Class clase) {

		traspaso = new Intent(this, clase);

	}
	
}